package com.suez.eimsserver.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.suez.eimsserver.entity.Department;
import com.suez.eimsserver.entity.Person;
import com.suez.eimsserver.entity.Protocol;

public class ReceiveMsgController {
	public static Protocol receive(Protocol protocol){
		Protocol protocol2 = new Protocol();
		Map<String, Object> message = new HashMap<String, Object>();
		int flag=0;
		String action = protocol.getAction();
		PersonController pController = new PersonController();
		DepartmentController deptController = new DepartmentController();
		
		Map<String, Object> msg = protocol.getMsg();
		switch (action) {
			case "add":
				if(msg.get("table")!=null){
					String table = (String)msg.get("table");
					if("person".equals(table)){
						Person person =(Person)msg.get("person");
						flag=pController.insert(person);
					}else if("dept".equals(msg.get("table"))){
						Department department=(Department)msg.get("dept");
						flag=deptController.insert(department);
					}
				}
				
				if(flag>0){
					message.put("msg", "success");
				}else {
					message.put("msg", "fail");
				}
				protocol2.setAction("add");
				protocol2.setMsg(message);
				break;
			case "delete":
				if(msg.get("table")!=null){
					String table = (String)msg.get("table");
					if("person".equals(table)){
						String id = (String)msg.get("person");
						flag=pController.delete(id);
					}else if("dept".equals(msg.get("table"))){
						String deptId=(String)msg.get("dept");
						flag=deptController.delete(deptId);
					}
				}
				if(flag>0){
					message.put("msg", "success");
				}else {
					message.put("msg", "fail");
				}
				protocol2.setAction("delete");
				protocol2.setMsg(message);
				break;
			case "update":
				if(msg.get("table")!=null){
					
					String table = (String)msg.get("table");
					if("person".equals(table)){
						
						Map<String, Object> map = (Map<String, Object>)msg.get("person");
						flag=pController.update(map);
					
					}else if("dept".equals(msg.get("table"))){
						
						Map<String, Object> map = (Map<String, Object>)msg.get("person");
						flag=deptController.update(map);
					}
				}
				if(flag>0){
					message.put("msg", "success");
				}else {
					message.put("msg", "fail");
				}
				protocol2.setAction("update");
				protocol2.setMsg(message);
				break;
			case "query":
					if(msg.get("table")!=null){
					
					String table = (String)msg.get("table");
					if("person".equals(table)){
						
						List<Person> list= pController.queryAll();
						message.put("person", list);
					}else if("dept".equals(msg.get("table"))){
				
						List<Department> list=deptController.queryAll();
						message.put("dept", list);
					}
				}
				protocol2.setAction("query");
				protocol2.setMsg(message);
				break;
			default:
				break;
		}
		return protocol2;
	}
	
	
}
